<template>
  <div class="divadd">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>黑马运营面经后台</span>
      </div>
      <el-form :rules="rules" :model="form" ref="formadd"  >
        <el-form-item prop="username" label="用户名：">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
         <el-form-item prop="password" label="密码：">
          <el-input v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
         <el-button @click="login" type="primary">登录</el-button>
         <el-button @click="logindel" plain>重置</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { onLogin } from '@/api/user'

// import { Message } from '@/utils/element'
export default {
  name: 'DayshiyPcIndex',

  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    login () {
      try {
        this.$refs.formadd.validate(async valid => {
          if (!valid) return

          const res = await onLogin(this.form)
          console.log(res)
          this.$store.commit('user/setusertoken', res.data.token)
          this.$message({
            message: '恭喜你，登录成功',
            type: 'success'
          })

          this.$router.push('/')
        })
      } catch (e) {
        this.$message.error('用户名或者密码错误')
        console.log(e)
      }
    },
    logindel () {
      this.$refs.formadd.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card {
  width: 420px;
  margin: 0 auto;
  ::v-deep .el-card__header{
    background-color: #727cf5;
    text-align: center;
    color: white;

  }
}
.divadd{
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
    display: flex;
  align-items: center;
  justify-content: space-around;
    min-height: 100vh;
}
</style>
